//
//  NETWorkingUtil.m
//  NiuBanJinGold
//
//  Created by zf on 15/10/28.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "NETWorkingUtil.h"
#import "Common.h"
#import "AppDelegate.h"
#import "LoginViewController.h"

@interface NETWorkingUtil ()
@property (nonatomic, strong) AFHTTPRequestOperationManager *manager;

@end


@implementation NETWorkingUtil
#pragma mark - AFnetworking manager getter

- (AFHTTPRequestOperationManager *) manager {
    
    if (!_manager) {
        
        _manager = [AFHTTPRequestOperationManager manager];
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        _manager.requestSerializer.timeoutInterval = 60;

        [_manager.securityPolicy setAllowInvalidCertificates:YES];
        [_manager.securityPolicy setValidatesDomainName:NO];
        [_manager.requestSerializer setValue:@"ios" forHTTPHeaderField:@"MOBILE_DEVICE"];
        [_manager.requestSerializer setValue:@"1.0.1" forHTTPHeaderField:@"CLIENT_VERSION"];
        [_manager.requestSerializer setValue:@"appstore" forHTTPHeaderField:@"CHANNEL_NAME"];
    }
    
    return _manager;
}

#pragma mark -sharedNETWorking
+(NETWorkingUtil *)sharedNETWorking {
    
    static NETWorkingUtil *networking = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        networking = [[NETWorkingUtil alloc]init];
    });
    return networking;
}

-(void)zzjrWallTrade_orderByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {

}

-(void)zzjrMore_moreInfoByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(More_activity_center) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"判断活动中心是否存在的返回值:%@==%@",dic,dic[@"msg"]);
        
         ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}

-(void)zzjrMore_userFeedbackInfoByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(More_userFeedback) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"意见反馈的返回值:%@==%@",dic,dic[@"msg"]);
        
         ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}


-(void)zzjrMore_myInvitationByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(More_myInvitation) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"我的邀请的返回值:%@==%@",dic,dic[@"msg"]);
        
         ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@" , error);
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}

-(void)zzjrMore_safeCenterByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(More_safeCenter) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"安全中心的返回值:%@==%@",dic,dic[@"msg"]);
        
         ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@" , error);
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}

-(void)zzjrMore_reset_passwordByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(More_resetPassword) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"忘记密码的返回值:%@==%@",dic,dic[@"msg"]);
        
         ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@" , error);
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}

-(void)zzjrMore_getCodeByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(Login_get_authentication_code) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"忘记获取验证码:%@==%@",dic,dic[@"msg"]);
        
         ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}

-(void)zzjrMore_change_passwordByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(More_changePassword) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"修改密码的返回值:%@==%@",dic,dic[@"msg"]);
        
         ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@" , error);
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}

-(void)zzjrMore_logoutByParmeteres:(NSDictionary *)paramsDic transferResultUsingBlock:(transferWebData)transfer {
    
    [self.manager POST:URLCOMBINE(More_logout) parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        
        NSLog(@"退出登录返回值:%@==%@",dic,dic[@"msg"]);
        
         ([dic[@"status"] integerValue] == 4)?( [self tokenExpired:dic[@"msg"]]): transfer(dic,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@" , error);
        if([error code] == NSURLErrorCancelled){
            
            NSLog(@"999 现了");
            return;
        }
        
        transfer(nil,error);
    }];
    
}

// 现阶段2B的解决TOKEN失效的方法
- (void) tokenExpired:(NSString *) msg {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"此账户已在其它设备上登录!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    [alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 0) {
        
        dispatch_async(dispatch_get_main_queue(), ^{

            [[UserManager sharedUserManager] logOut];
        });
        
        AppDelegate *delegate =  [[UIApplication sharedApplication] delegate];
        
        UINavigationController *nav_token_expired = [[UINavigationController alloc] initWithRootViewController:[LoginViewController new]];
        
        delegate.window.rootViewController = nav_token_expired;
    }
}

@end
